package net.lahuobao.mall.service.impl.admin;


import net.lahuobao.mall.dao.admin.MallOilCardRecoveryMapper;
import net.lahuobao.mall.dao.admin.MallShopCardruleMapper;
import net.lahuobao.mall.dao.admin.MallTransProjectEmployeeMapper;
import net.lahuobao.mall.entity.admin.*;
import net.lahuobao.mall.service.admin.MallProjectUpdateLogServiceI;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.mall.service.admin.MallShopCardruleServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("mallShopCardruleService")
@Transactional
public class MallShopCardruleServiceImpl extends CommonServiceImpl implements MallShopCardruleServiceI {

    @Autowired
    private MallOilCardRecoveryMapper mallOilCardRecoveryMapper;

    @Autowired
    private MallProjectUpdateLogServiceI logService;

    @Autowired
    private MallShopCardruleMapper mallShopCardruleMapper;

    @Autowired
    private MallTransProjectEmployeeMapper mallTransProjectEmployeeMapper;

    @Override
    public void save(MallShopCardruleEntity mallShopCardrule) {
        //计算油卡
        Integer startCardNo = Integer.valueOf(mallShopCardrule.getStartCardNo());
        Integer endCardNo = Integer.valueOf(mallShopCardrule.getEndCardNo());
        List<MallShopCardruleEntity> cardModelList = new ArrayList<MallShopCardruleEntity>();

        int length = endCardNo.toString().length();
        for (int i = startCardNo; i <= endCardNo; i++) {
            MallShopCardruleEntity param = new MallShopCardruleEntity();
            //创建人
            param.setCreateuserid(mallShopCardrule.getCreateuserid());
            //创建时间
            param.setCreatetime(new Date());
            //店铺
            param.setShopid(mallShopCardrule.getShopid());
            //供应商
            param.setOilsupplierid(mallShopCardrule.getOilsupplierid());
            //商品
            param.setProductid(mallShopCardrule.getProductid());
            //卡号
            String cardNo = mallShopCardrule.getCardNoPrefix();
            //长度不相等，补位
            String temp = String.valueOf(i);
            if (String.valueOf(i).length() != String.valueOf(endCardNo).length()) {
                String str ="0000000000";
                Integer tempLength =  String.valueOf(endCardNo).length() - String.valueOf(i).length();
                cardNo += str.substring(0, tempLength) + i;
            }else{
                cardNo = cardNo + i;
            }
            param.setCardno(cardNo);
            try {
                if(mallShopCardrule.getProjectUserId()>-1) {
                    param.setProjectUserId(mallShopCardrule.getProjectUserId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            cardModelList.add(param);
        }
        super.batchSave(cardModelList);
    }

    /**
     * 查看油卡回收
     * @param mallOilCardRecovery
     * @return
     */
    @Override
    public List<MallOilCardRecovery> selectOilCardRecovery(MallOilCardRecovery mallOilCardRecovery) {
        //项目为空，设置权限
        if(StringUtil.isEmpty(mallOilCardRecovery.getProjects())){
            //判断权限
            HttpSession session = ContextHolderUtils.getSession();
            TSUser user = (TSUser)session.getAttribute(ResourceUtil.LOCAL_CLINET_USER);
            List<MallProjectUserBaseEntity> lists = super.findByProperty(MallProjectUserBaseEntity.class, "accountid", user.getId());
            //admin
            List<Integer> project = new ArrayList<Integer>();
            List<Integer> projectUser = new ArrayList<Integer>();
            String paramProject = "";
            String paramProjectUser = "";
            for(MallProjectUserBaseEntity  temp : lists){
                //判断是否被删除
                if(!temp.getIsdelete()){
                    //是否是管理员
                    if(temp.getIsadmin()){
                        //查询出该项目组
                        project.add(temp.getProjectid());
                    }else{
                        //只查询出关联该人的油卡
                        projectUser.add(temp.getId());
                    }
                }
            }
            for(int i = 0; i < project.size(); i++){
                if(i == project.size() - 1){
                    paramProject = paramProject + project.get(i);
                }else{
                    paramProject = paramProject + project.get(i) + ",";
                }
            }
            for(int i = 0; i < projectUser.size(); i++){
                if(i == projectUser.size() - 1){
                    paramProjectUser = paramProjectUser + projectUser.get(i);
                }else{
                    paramProjectUser = paramProjectUser + projectUser.get(i) + ",";
                }
            }
            if(lists.size() > 0){
                mallOilCardRecovery.setProjects(paramProject);
                mallOilCardRecovery.setProjectUsers(paramProjectUser);
            }
        }
        return mallOilCardRecoveryMapper.selectOilCardRecovery(mallOilCardRecovery, getPageBounds());
    }

    /**
     * 批量更新状态
     * @param ids
     * @param state
     */
    @Override
    public void batchUpState(String ids, Integer state) {
        List<MallProjectUpdateLogEntity> logEntities = new ArrayList<MallProjectUpdateLogEntity>();
        List<Integer> listIds = new ArrayList<Integer>();
        String[] intIds = ids.split(",");
        HttpSession session = ContextHolderUtils.getSession();
        TSUser user = (TSUser)session.getAttribute(ResourceUtil.LOCAL_CLINET_USER);
        for(String temp : intIds){
            listIds.add(Integer.valueOf(temp));
            MallShopCardrule oilCardEntity = mallShopCardruleMapper.selectByPrimaryKey(Integer.valueOf(temp));
            MallProjectUpdateLogEntity logEntity = new MallProjectUpdateLogEntity();
            //类型为 油卡
            logEntity.setLogtype(2);
            //油卡
            logEntity.setCardno(oilCardEntity.getCardno());
            //编辑人
            logEntity.setUpdateuserid(user.getId());
            //编辑内容
            logEntity.setLogcontent("将属性状态 "+ getStateName(oilCardEntity.getState()) +" 修改为 " + getStateName(state));
            //时间
            logEntity.setUpdatetime(new Date());
            logEntities.add(logEntity);
        }
        //批量更改状态
        mallOilCardRecoveryMapper.batchUpState(listIds, state);
        //写入日志记录
        logService.batchSave(logEntities);
    }

    /**
     * 获取状态名字
     * @param state
     * @return
     */
    private String getStateName(Integer state){
        String name = "";
        switch (state){
            case 0:
                name = "未回收";
                break;
            case 1:
                name = "已回收";
                break;
            case 3:
                name = "已挂失";
                break;
        }
        return name;
    }

    /**
     * 查看油卡使用记录
     * @param mallOilCardRecovery
     * @return
     */
    @Override
    public List<MallOilCardRecovery> selectOilCardRecoveryByCardNo(MallOilCardRecovery mallOilCardRecovery) {
        return mallOilCardRecoveryMapper.selectOilCardRecoveryByCardNo(mallOilCardRecovery, getPageBounds());
    }

    /**
     * 统计油卡回收情况
     * @return
     */
    @Override
    public MallOilCardRecoveryUse StatisticsOilCardRecoveryUse() {
        return mallOilCardRecoveryMapper.StatisticsOilCardRecoveryUse();
    }

    @Override
    public List<MallShopCardruleQuery> selectMallShopCardRule(Map map, DataGrid dataGrid){
    //    List<MallTransProjectEmployee> accountList = mallTransProjectEmployeeMapper.getAccountEmployees((String) map.get("accountId"));
//       if(accountList!=null && accountList.size()>0){
//           map.put("isExits","true");
//       }
        List<MallShopCardruleQuery> mallShopCardruleQueryList = mallShopCardruleMapper.selectMallShopCardRule(map);
        int count = mallShopCardruleMapper.selectMallShopCardRuleCount(map);
        dataGrid.setResults(mallShopCardruleQueryList);
        dataGrid.setTotal(count);
        return mallShopCardruleQueryList;
    }

    @Override
    public List<MallShopCardruleQuery> getMallShopCardInfo(String cardNo) {
        if (StringUtil.isNotEmpty(cardNo)) {
            return mallShopCardruleMapper.getMallShopCardInfo(cardNo);
        }
        return null;
    }

    @Override
    public MallShopCardrule getMallShopCardRule(String cardNo) {
        return mallShopCardruleMapper.getMallShopCardrule(cardNo);
    }

    @Override
    public void saveMallShopCardRule(MallShopCardrule mallShopCardrule) {
        mallShopCardruleMapper.updateByPrimaryKey(mallShopCardrule);
    }

    @Override
    public MallShopCardrule selectById(Integer id) {
        return mallShopCardruleMapper.selectByPrimaryKey(id);
    }
}